import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:svg_flutter/svg.dart';
import 'package:window_example/util/print_util.dart';
import 'package:window_example/widget/window_button.dart';
import 'package:window_manager/window_manager.dart';

import '../constant/constant.dart';
import '../util/window_util.dart';

class TitleBarWidget extends StatefulWidget {
  const TitleBarWidget({super.key});

  @override
  State<TitleBarWidget> createState() => _TitleBarWidgetState();
}

class _TitleBarWidgetState extends State<TitleBarWidget> with WindowListener {
  bool isMaximized = false;
  static const TAG = "TitleBarWidgetState";

  @override
  void initState() {
    // TODO: implement initState
    windowManager.addListener(this);
    super.initState();
    windowManager.isMaximized().then((value) => isMaximized = value);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    windowManager.removeListener(this);
  }

  @override
  Widget build(BuildContext context) {
    var iconSize = const Size(14, 14);
    var backgroundSize = const Size(30, 30);
    var maxButtonIcons = WindowButtonIcons(
        SvgPicture.asset('assets/images/max.svg'),
        SvgPicture.asset('assets/images/max.svg'),
        SvgPicture.asset('assets/images/max.svg'));

    var minButtonIcons = WindowButtonIcons(
        SvgPicture.asset('assets/images/min.svg'),
        SvgPicture.asset('assets/images/min.svg'),
        SvgPicture.asset('assets/images/min.svg'));
    var closeButtonIcons = WindowButtonIcons(
        SvgPicture.asset('assets/images/close.svg'),
        SvgPicture.asset('assets/images/close_down.svg'),
        SvgPicture.asset('assets/images/close_mouse_over.svg'));
    return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        mainAxisSize: MainAxisSize.max,
        children: [
          const Expanded(
              child: DragToMoveArea(
            child: Text(""),
          )),
          Align(
            alignment: Alignment.center,
            child: Row(
              children: [
                MinimizeWindowButton(
                  backgroundSize: backgroundSize,
                  iconSize: iconSize,
                  icons: minButtonIcons,
                ),
                (!isMaximized)
                    ? MaximizeWindowButton(
                        backgroundSize: backgroundSize,
                        iconSize: iconSize,
                        icons: maxButtonIcons,
                        onPressed: () {
                          LoggerUtil.d(TAG, "MaximizeWindowButton onPressed");
                          windowManager.maximize();
                        },
                      )
                    : RestoreWindowButton(
                        backgroundSize: backgroundSize,
                        iconSize: iconSize,
                        onPressed: () {
                          LoggerUtil.d(TAG, "RestoreWindowButton onPressed");
                          windowManager.restore();
                        },
                      ),
                CloseWindowButton(
                  backgroundSize: backgroundSize,
                  iconSize: iconSize,
                  icons: closeButtonIcons,
                ),
              ],
            ),
          )
        ]);

    DragToMoveArea(
        child: Container(
      padding: EdgeInsets.all(titleBarTopAndBottomMargin),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        mainAxisSize: MainAxisSize.max,
        children: [
          //       Expanded(
          //         //   child: DragToMoveArea(
          //         // child: Container(
          //         //   width: 800,
          //         //   color: Colors.red,
          //         //   child: Text("ssss"),
          //         // ),
          //       // )
          // ),
          MinimizeWindowButton(
            backgroundSize: backgroundSize,
            iconSize: iconSize,
            icons: minButtonIcons,
          ),
          (!isMaximized)
              ? MaximizeWindowButton(
                  backgroundSize: backgroundSize,
                  iconSize: iconSize,
                  icons: maxButtonIcons,
                  onPressed: () {
                    LoggerUtil.d(TAG, "MaximizeWindowButton onPressed");
                    windowManager.maximize();
                  },
                )
              : RestoreWindowButton(
                  backgroundSize: backgroundSize,
                  iconSize: iconSize,
                  onPressed: () {
                    LoggerUtil.d(TAG, "RestoreWindowButton onPressed");
                    windowManager.restore();
                  },
                ),
          CloseWindowButton(
            backgroundSize: backgroundSize,
            iconSize: iconSize,
            icons: closeButtonIcons,
          ),
        ],
      ),
    ));
  }

  @override
  void onWindowMaximize() {
    // TODO: implement onWindowMaximize
    super.onWindowMaximize();
    LoggerUtil.d(TAG, "onWindowMaximize");
    isMaximized = true;
    setState(() {});
  }

  @override
  void onWindowRestore() {
    // TODO: implement onWindowRestore
    super.onWindowRestore();
    LoggerUtil.d(TAG, "onWindowRestore");
    isMaximized = false;
    setState(() {});
  }

  @override
  void onWindowUnmaximize() {
    // TODO: implement onWindowUnmaximize
    super.onWindowUnmaximize();
    LoggerUtil.d(TAG, "onWindowRestore");
    isMaximized = false;
    setState(() {});
  }
}
